package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdchannelMapper;
import com.usian.admin.service.AdchannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.nio.channels.Channel;
import java.util.Date;
import java.util.List;

@Service
public class AdchannelServiceImpl extends ServiceImpl<AdchannelMapper, AdChannel> implements AdchannelService {

    @Resource
    private AdchannelMapper adchannelMapper;

    @Override
    public ResponseResult findBynameandpage(ChannelDto channelDto) {
        /*
        * 判断参数是否为空
        * */
        if(channelDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        /*
        * 校验参数
        * */
        channelDto.checkParam();

        /*
        * 进行分页
        * */
        Page page = new Page(channelDto.getPage(), channelDto.getSize());
        LambdaQueryWrapper<AdChannel> q = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(channelDto.getName())){
            q.like(AdChannel::getName,channelDto.getName());
        }
        IPage page1 = page(page, q);

        /*
        *
        * 参数封装
        * */
        ResponseResult responseResult = new PageResponseResult(channelDto.getPage(),channelDto.getSize(),(int)page1.getTotal());
        responseResult.setData(page1.getRecords());


        return responseResult;
    }

    /*
    * 添加adchannel方法
    * */
    @Override
    public ResponseResult add(AdChannel adChannel) {
        //判断参数是否为空
        if(adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //赋值时间
        adChannel.setCreatedTime(new Date());
        //完成添加
        boolean save = save(adChannel);

        //赋值返回
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult del(Integer id) {

        AdChannel byId = getById(id);
        //判断是否可以删除
        if(byId.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //完成删除
        boolean b = removeById(id);

        //完成赋值
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult edit(AdChannel adChannel) {
        //判断参数是否为空
        if(adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //完成修改
        boolean b = updateById(adChannel);
        //完成赋值返回
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult findAll() {
        List<AdChannel> adChannels = adchannelMapper.selectList(null);
        return ResponseResult.okResult(adChannels);
    }
}
